package com.qy127.javaweb.controller;


import com.qy127.javaweb.dao.impl.TypeImpl;
import com.qy127.javaweb.entity.Type;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/typ/*")
public class TypeServlet extends HttpServlet {
    private final TypeImpl typeImpl = new TypeImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //请求得到请求的路径
        String i = req.getRequestURI();
        //获取url的路径最后一位下标
        int i1 = i.lastIndexOf("/");
        //截取字符串中的字符  截取i1后面的字符 并作为String
        String substring = i.substring(i1 + 1);
        switch (substring) {
            case "type":
                chaxun(req, resp);
                break;
            case "add":
                addtype(req, resp);
            case "del":
                deltype(req, resp);
            case "xiu":
                xiugai(req, resp);
            case "xadd":
                xadd(req,resp);
            default:
                break;
        }

    }

    protected void chaxun(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Type> type = typeImpl.type();
        req.setAttribute("type", type);
        req.getRequestDispatcher("/type.jsp").forward(req, resp);
    }


    public void addtype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String parameter1 = req.getParameter("typeid");
        String parameter2 = req.getParameter("typename");
//        int addtype = typemlpl.addtype(Integer.parseInt(parameter1), parameter2);
        int addtype = typeImpl.addtype(null, parameter2);
//        System.out.println(addtype);
        if (addtype > 0) {
            req.setAttribute("addtype", "添加成功");
        } else {
            req.setAttribute("addtype", "添加失败");
        }
        req.getRequestDispatcher("/addtype.jsp").forward(req, resp);
    }

    protected void deltype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("typeid");
        int deltype = typeImpl.deltype(Integer.parseInt(id));
        // System.out.println(deltype);
        if (deltype > 0) {
            chaxun(req, resp);
        }
    }

    public void xiugai(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String parameter = req.getParameter("typeid");//获得修改中的参数
//        List xg = typemlpl.xg(Integer.parseInt(parameter));//将参数带入查询数据的方法中\
        List<Type> xg = typeImpl.xg(Integer.parseInt(parameter));
        req.setAttribute("typeid",xg.get(0).getTypeid());
        req.setAttribute("typename", xg.get(0).getTypename());//将得到的数据装在名为xiusj的数据中  然后将数据放在jsp中
        req.getRequestDispatcher("/xiugai.jsp").forward(req, resp);
//        System.out.println(xg);
    }

    public void xadd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String typeid1 = req.getParameter("typeid");
        String id = req.getParameter("id");
        String typeid2 = req.getParameter("typename");
        int xadd = typeImpl.xadd(Integer.parseInt(id),typeid2,Integer.parseInt(typeid1));
        if (xadd>0){
            chaxun(req,resp);
        }
    }
}
